package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;


@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //新用户
        Integer newUsers = userMapper.countByMap(map);
        //订单总数
        Integer orderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        //有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);
        //订单完成率
        Double orderCompletionRate = 0.0;
        if (orderCount != 0){
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }
        //平均客单价
        Double unitPrice = 0.0;
        if (turnover != 0){
            unitPrice = turnover / validOrderCount;
        }
        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }


    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {

        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);

        Map map = new HashMap();
        map.put("begin",begin);

        Integer allOrders = orderMapper.countByMap(map);
        //已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);
        //已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);
        //待派送数量
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);
        //待接单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }


    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverViewVO() {

        Map map = new HashMap();
        map.put("status", StatusConstant.DISABLE);
        //已停售菜品
        Integer discontinued = dishMapper.countByMap(map);

        map.put("status", StatusConstant.ENABLE);
        //已起售菜品
        Integer sold = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }


    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverViewVO() {
        Map map = new HashMap();
        map.put("status", StatusConstant.DISABLE);
        //已停售套餐
        Integer discontinued = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.ENABLE);
        //已起售套餐
        Integer sold = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
